from pydantic import BaseModel, Field
from pydantic_settings import BaseSettings
from typing import List
import yaml
import os

class FeedConfig(BaseModel):
    name: str
    rss: str

class FnsaNewsManagerConfig(BaseModel):
    scrape_interval_seconds: int
    log_level: str = "INFO"  # Default log level
    feeds: List[FeedConfig]

class AppConfig(BaseSettings):
    config_file_path: str = Field(..., env='CONFIG_FILE_PATH')
    etcd_host: str = Field(..., env='ETCD_HOST')
    etcd_port: int = Field(..., env='ETCD_PORT')
    parse_only_new_items: bool = Field(default=True, env='PARSE_ONLY_NEW_ITEMS')
    cut_off_seconds: int = Field(default=0, env='CUT_OFF_SECONDS')
    
    class Config:
        env_file = '.env'

def load_config() -> FnsaNewsManagerConfig:
    app_config = AppConfig()
    
    with open(app_config.config_file_path, 'r') as file:
        config_dict = yaml.safe_load(file)
    
    fnsa_news_manager_config = config_dict.get('fnsa-news-manager')
    
    if fnsa_news_manager_config is None:
        raise ValueError("fnsa-news-manager configuration is missing")
    
    fnsa_news_manager_config = FnsaNewsManagerConfig(**fnsa_news_manager_config)
    
    return fnsa_news_manager_config, app_config.etcd_host, app_config.etcd_port, app_config.parse_only_new_items, app_config.cut_off_seconds
